package com.study.happystudy.tool;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class PDFConverter {
    /**
     * PDF转图片
     * @param pdfFile
     * @return
     * @throws IOException
     */
    public static MultipartFile convertFirstPageToImage(MultipartFile pdfFile) throws IOException {
//        // Convert MultipartFile to InputStream
//        InputStream pdfInputStream = pdfFile.getInputStream();
//
//        // Load PDF document using PDFBox
//        PDDocument document = PDDocument.load(pdfInputStream);
//
//        // Create PDFRenderer object to render the first page
//        PDFRenderer pdfRenderer = new PDFRenderer(document);
//
//        // Render the first page as a BufferedImage
//        BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 300); // 300 DPI
//
//        // Convert BufferedImage to byte array
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        ImageIO.write(bufferedImage, "png", baos);
//        byte[] imageBytes = baos.toByteArray();
//
//        // Wrap byte array in a MultipartFile
//        MultipartFile imageFile = new MultipartFile() {
//            @Override
//            public String getName() {
//                return pdfFile.getName();
//            }
//
//            @Override
//            public String getOriginalFilename() {
//                return pdfFile.getOriginalFilename().replace(".pdf", ".png");
//            }
//
//            @Override
//            public String getContentType() {
//                return "image/png";
//            }
//
//            @Override
//            public boolean isEmpty() {
//                return false;
//            }
//
//            @Override
//            public long getSize() {
//                return imageBytes.length;
//            }
//
//            @Override
//            public byte[] getBytes() throws IOException {
//                return imageBytes;
//            }
//
//            @Override
//            public InputStream getInputStream() throws IOException {
//                return pdfFile.getInputStream();
//            }
//
//            @Override
//            public void transferTo(java.io.File dest) throws IOException, IllegalStateException {
//                pdfFile.transferTo(dest);
//            }
//        };
//
//        // Close the document
//        document.close();
//
//        return imageFile;
        try (InputStream pdfInputStream = pdfFile.getInputStream();
             PDDocument document = PDDocument.load(pdfInputStream)) {

            PDFRenderer pdfRenderer = new PDFRenderer(document);
            BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 300); // 300 DPI

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "png", baos);
            byte[] imageBytes = baos.toByteArray();

            // Create an InputStream from the byte array
            ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);

            // Wrap byte array in a MultipartFile implementation
            return new MultipartFile() {
                @Override
                public String getName() {
                    return pdfFile.getName();
                }

                @Override
                public String getOriginalFilename() {
                    return pdfFile.getOriginalFilename().replace(".pdf", ".png");
                }

                @Override
                public String getContentType() {
                    return "image/png";
                }

                @Override
                public boolean isEmpty() {
                    return false;
                }

                @Override
                public long getSize() {
                    return imageBytes.length;
                }

                @Override
                public byte[] getBytes() throws IOException {
                    return imageBytes;
                }

                @Override
                public InputStream getInputStream() throws IOException {
                    return bais; // Use ByteArrayInputStream for image data
                }

                @Override
                public void transferTo(File dest) throws IOException, IllegalStateException {
                    try (OutputStream fos = new FileOutputStream(dest)) {
                        fos.write(imageBytes);
                    }
                }
            };
        }
    }
}
